//
//  TZShowVoiceView.m
//  BSBDJ
//
//  Created by user on 16/5/27.
//  Copyright © 2016年 Burning. All rights reserved.
//

#import "TZShowVoiceView.h"
#import "TZEssenceItem.h"
#import <AVFoundation/AVFoundation.h>
@interface TZShowVoiceView()
@property (weak, nonatomic) IBOutlet UIButton *playvoice_Btn;
@property (weak, nonatomic) IBOutlet UILabel *time_Lable;
@property (weak, nonatomic) IBOutlet UILabel *playcount_Label;
@property (weak, nonatomic) IBOutlet UIImageView *pic_ImageV;
@property (nonatomic, strong) AVPlayerItem *playerItem;
@property (nonatomic, strong)  AVPlayer* player ;
@end


@implementation TZShowVoiceView


+ (instancetype)showVoiceView{
    return [[NSBundle mainBundle] loadNibNamed:@"TZShowVoiceView" owner:nil options:nil].lastObject;
}
- (IBAction)clickPlayVioce:(UIButton *)sender {
    sender.selected  =!sender.selected;
    if (sender.selected) {
        _playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:self.item.voiceuri]];
        _player = [AVPlayer playerWithPlayerItem:_playerItem];
        
        [_player play];
    }else{
        [_player pause];
    }
}

- (void)setItem:(TZEssenceItem *)item{
    _item = item;
    
    [self.pic_ImageV sd_setImageWithURL:[NSURL URLWithString:item.large_image]];
    
    self.time_Lable.text = [self getFormatterTime:item.voicetime];
    self.playcount_Label.text = [self getForamttPlaycount:item.playcount];
    NSLog(@"%@---%ld",self.time_Lable.text,(long)item.playcount
          );
    NSLog(@"%@",self.item.middle_image);
}

- (NSString *)getForamttPlaycount :(CGFloat)playcount{
    NSString *countStr;
    if (playcount > 10000) {
        countStr = [NSString stringWithFormat:@"%.1f万",playcount / 10000];
    }else{
        countStr = [NSString stringWithFormat:@"%f",playcount];
    }
    return countStr;
}

- (NSString *)getFormatterTime :(int)time{
    
    int sec = time % 60;
    int min = time / 60;
    NSString *newTime = [NSString stringWithFormat:@"%02d:%02d",min,sec];
    return newTime;
}

@end
